*****
global master_folder "/Users/christopherrauh/Dropbox/JEEA_replication/"
cd "$master_folder"


**optimal intervention cutoff

clear all


*new trial run
local f=1
local topics=30
insheet using "RandomForest1_3_200_0_superall_topics`topics'_0_`f'.csv", clear


*now any violence graphs
*preserve
keep if dep=="ons_anyviolence`f'"


generate time=ym(year, month)
format time %tm
egen countryid = group(isocode)
tsset countryid time

*rename randomforest mine
gsort - mine
drop if ons_anyviolence`f'==.
drop if mine==.

generate positives = sum(ons_anyviolence`f' )

generate negatives = sum(1-ons_anyviolence`f' )

egen allpositives = total(ons_anyviolence`f' )

egen allnegatives = total(1-ons_anyviolence`f' )

generate tpr = positives/allpositives

 order isocode year month mine positives negatives allpositives allnegatives tpr


*save foralessandro.dta, replace


generate hardcases= anyviolence_dp>120 & anyviolence_dp!=.

generate positives_hard = sum(ons_anyviolence`f'*hardcases )

generate negatives_hard = sum((1-ons_anyviolence`f')*hardcases )

egen allpositives_hard = total(ons_anyviolence`f'*hardcases )

egen allnegatives_hard = total((1-ons_anyviolence`f')*hardcases )


generate textcases= anyviolence_dp>23 & anyviolence_dp!=.

matrix results=(0,0,0, 0, 0)

forvalues d=0/100 {

local pvar=0.25

********************
*start cost minimization here
local p=`pvar'
local vD=10*`d'
local vP=0
local I=1

*decision making
*cost benchmark works as follows
*preventing conflict helps prevent massive damages (calculated for infinite horizon)
matrix costvector=(`vP',`vP'+`I',(1-`p')*`vD'+`p'*`vP'+`I',`vD')
*TN, FP, TP, FN

*define cost function
local cTN=costvector[1,1]
local cFP = costvector[1,2]
local cTP = costvector[1,3]
local cFN = costvector[1,4]

*negatives are false positives (they are actually negatives but at this cutoff we think they are positives)
generate costs_int = negatives*`cFP'+positives*`cTP'+(allpositives-positives)*`cFN'+(allnegatives-negatives)*`cTN'
generate costs_noint = (allpositives)*`cFN'+(allnegatives)*`cTN'

generate costsaving=costs_int-costs_noint

*hard cases
local p=`pvar'
local vD=10*`d'
local vP=0
local I=0.1

*decision making
*cost benchmark works as follows
*preventing conflict helps prevent massive damages (calculated for infinite horizon)
matrix costvector=(`vP',`vP'+`I',(1-`p')*`vD'+`p'*`vP'+`I',`vD')
*TN, FP, TP, FN

*define cost function
local cTN=costvector[1,1]
local cFP = costvector[1,2]
local cTP = costvector[1,3]
local cFN = costvector[1,4]


*hard cases costs
generate costs_int_hard = negatives_hard*`cFP'+positives_hard*`cTP'+(allpositives_hard-positives_hard)*`cFN'+(allnegatives_hard-negatives_hard)*`cTN'
generate costs_noint_hard = (allpositives_hard)*`cFN'+(allnegatives_hard)*`cTN'

generate costsaving_hard=costs_int_hard-costs_noint_hard


****************************
*less effective intervention

*all cases
local p=`pvar'/5
local vD=10*`d'
local vP=0
local I=1

*decision making
*cost benchmark works as follows
*preventing conflict helps prevent massive damages (calculated for infinite horizon)
matrix costvector=(`vP',`vP'+`I',(1-`p')*`vD'+`p'*`vP'+`I',`vD')

local cTN=costvector[1,1]
local cFP = costvector[1,2]
local cTP = costvector[1,3]
local cFN = costvector[1,4]

*negatives are false positives (they are actually negatives but at this cutoff we think they are positives)
generate costs_int_lowp = negatives*`cFP'+positives*`cTP'+(allpositives-positives)*`cFN'+(allnegatives-negatives)*`cTN'
generate costs_noint_lowp = (allpositives)*`cFN'+(allnegatives)*`cTN'

generate costsaving_lowp=costs_int_lowp-costs_noint_lowp


*hard cases less worth it
local p=`pvar'/5
local vD=10*`d'
local vP=0
local I=0.1

*decision making
*cost benchmark works as follows
*preventing conflict helps prevent massive damages (calculated for infinite horizon)
matrix costvector=(`vP',`vP'+`I',(1-`p')*`vD'+`p'*`vP'+`I',`vD')

local cTN=costvector[1,1]
local cFP = costvector[1,2]
local cTP = costvector[1,3]
local cFN = costvector[1,4]



*hard cases costs
generate costs_int_hard_lowp = negatives_hard*`cFP'+positives_hard*`cTP'+(allpositives_hard-positives_hard)*`cFN'+(allnegatives_hard-negatives_hard)*`cTN'
generate costs_noint_hard_lowp = (allpositives_hard)*`cFN'+(allnegatives_hard)*`cTN'

 

generate costsaving_hard_lowp=costs_int_hard_lowp-costs_noint_hard_lowp




*get cutoff value
egen mincost = min(costs_int)
egen mincost_lowp = min(costs_int_lowp)

generate prevvalue=(costs_noint-mincost)/costs_noint
summarize prevvalue
local prevvalue=r(mean)

generate prevvalue_lowp=(costs_noint_lowp-mincost_lowp)/costs_noint_lowp
summarize prevvalue_lowp
local  prevvalue_lowp=r(mean)



generate cutoff = mincost==costs_int
generate cutoff_lowp = mincost_lowp==costs_int_lowp

generate cutoffvalue = mine if cutoff==1
egen cutoffv = max(cutoffvalue)
summarize cutoffv
local cutoffv=r(mean)

generate cutoffvalue_lowp = mine if cutoff_lowp==1
egen cutoffv_lowp = max(cutoffvalue_lowp)
summarize cutoffv_lowp
local cutoffv_lowp = r(mean)

drop costs*
drop cutoff*
drop mincos*
drop prevvalue*

local damage= 10*`d'
matrix result=(`damage', `cutoffv', `cutoffv_lowp', `prevvalue', `prevvalue_lowp')
matrix results=results \ result

}
clear
svmat results
drop in 1


*Figure D1

graph drop _all
twoway (line  results2 results1, lwidth(thick) lpattern(solid) ) (line  results3 results1, lwidth(thick) lpattern(dash)) if results1<500, ///
 ytitle(`"Optimal intervention cutoff"') subtitle("Intervention cutoffs with rising conflict damage", color(black)) xtitle(`"Conflict damage per month"') legend( order(2 "Low effectiveness" 1 "High effectiveness") size(small)) ///
plotregion(fcolor(white)) graphregion(fcolor(white))  name(figa)


twoway (line  results4 results1, lwidth(thick) lpattern(solid)) (line  results5 results1, lwidth(thick) lpattern(dash)) if results1<500, ///
subtitle("Prevented damage with rising conflict damage", color(black)) ytitle("Share of conflict damage prevented") xtitle(`"Conflict damage per month"') legend( order(2 "Low Effectiveness" 1 "High Effectiveness") size(small)) ///
plotregion(fcolor(white)) graphregion(fcolor(white))  name(figb)

grc1leg figa figb, xcommon legendfrom(figa) plotregion(fcolor(white)) graphregion(fcolor(white)) cols(1) 

graph export "Figures_superall/intervention_cutoffs_prevdam.pdf" ,replace



**optimal intervention cutoff

clear all


*new trial run
local f=1
local topics=30
insheet using "RandomForest1_3_200_0_superall_topics`topics'_0_`f'.csv", clear


*now any violence graphs
*preserve
keep if dep=="ons_anyviolence`f'"



generate time=ym(year, month)
format time %tm
egen countryid = group(isocode)
tsset countryid time

*rename randomforest mine
gsort - mine
drop if ons_anyviolence`f'==.
drop if mine==.

generate positives = sum(ons_anyviolence`f' )

generate negatives = sum(1-ons_anyviolence`f' )

egen allpositives = total(ons_anyviolence`f' )

egen allnegatives = total(1-ons_anyviolence`f' )

generate tpr = positives/allpositives
generate fpr=negatives/allnegatives


 order isocode year month mine positives negatives allpositives allnegatives tpr fpr


*save foralessandro.dta, replace


generate hardcases= anyviolence_dp>120 & anyviolence_dp!=.

generate positives_hard = sum(ons_anyviolence`f'*hardcases )

generate negatives_hard = sum((1-ons_anyviolence`f')*hardcases )

egen allpositives_hard = total(ons_anyviolence`f'*hardcases )

egen allnegatives_hard = total((1-ons_anyviolence`f')*hardcases )


generate textcases= anyviolence_dp>23 & anyviolence_dp!=.


local pvar=0.25

********************
*start cost minimization here
local p=`pvar'
local vD=100
local vP=0
local I=1

*decision making
*cost benchmark works as follows
*preventing conflict helps prevent massive damages (calculated for infinite horizon)
matrix costvector=(`vP',`vP'+`I',(1-`p')*`vD'+`p'*`vP'+`I',`vD')
*TN, FP, TP, FN

*define cost function
local cTN=costvector[1,1]
local cFP = costvector[1,2]
local cTP = costvector[1,3]
local cFN = costvector[1,4]

*negatives are false positives (they are actually negatives but at this cutoff we think they are positives)
generate costs_int = negatives*`cFP'+positives*`cTP'+(allpositives-positives)*`cFN'+(allnegatives-negatives)*`cTN'
generate costs_noint = (allpositives)*`cFN'+(allnegatives)*`cTN'

generate costsaving=costs_int-costs_noint
egen mincost_intervention=min(costs_int)

generate cTN=costvector[1,1]
generate cFP = costvector[1,2]
generate cTP = costvector[1,3]
generate cFN = costvector[1,4]

generate isocost_tpr=fpr*(allnegatives/allpositives)*(cFP-cTN)/(cFN-cTP)-((mincost_intervention/allnegatives)-cTN-cFN* /// 
allpositives/allnegatives)*(allnegatives/allpositives)/(cFN-cTP)




*hard cases
local p=`pvar'
local vD=100
local vP=0
local I=0.1

*decision making
*cost benchmark works as follows
*preventing conflict helps prevent massive damages (calculated for infinite horizon)
matrix costvector=(`vP',`vP'+`I',(1-`p')*`vD'+`p'*`vP'+`I',`vD')
*TN, FP, TP, FN

*define cost function
local cTN=costvector[1,1]
local cFP = costvector[1,2]
local cTP = costvector[1,3]
local cFN = costvector[1,4]


*hard cases costs
generate costs_int_hard = negatives_hard*`cFP'+positives_hard*`cTP'+(allpositives_hard-positives_hard)*`cFN'+(allnegatives_hard-negatives_hard)*`cTN'
generate costs_noint_hard = (allpositives_hard)*`cFN'+(allnegatives_hard)*`cTN'

generate costsaving_hard=costs_int_hard-costs_noint_hard


****************************
*less effective intervention

*all cases
local p=`pvar'/5
local vD=100
local vP=0
local I=1

*decision making
*cost benchmark works as follows
*preventing conflict helps prevent massive damages (calculated for infinite horizon)
matrix costvector=(`vP',`vP'+`I',(1-`p')*`vD'+`p'*`vP'+`I',`vD')

local cTN=costvector[1,1]
local cFP = costvector[1,2]
local cTP = costvector[1,3]
local cFN = costvector[1,4]

*negatives are false positives (they are actually negatives but at this cutoff we think they are positives)
generate costs_int_lowp = negatives*`cFP'+positives*`cTP'+(allpositives-positives)*`cFN'+(allnegatives-negatives)*`cTN'
generate costs_noint_lowp = (allpositives)*`cFN'+(allnegatives)*`cTN'

generate costsaving_lowp=costs_int_lowp-costs_noint_lowp



*****isocost calculations
egen mincost_intervention_lowp=min(costs_int_lowp)

generate cTN_lowp=costvector[1,1]
generate cFP_lowp = costvector[1,2]
generate cTP_lowp = costvector[1,3]
generate cFN_lowp = costvector[1,4]

generate isocost_tpr_lowp=fpr*(allnegatives/allpositives)*(cFP_lowp-cTN_lowp)/(cFN_lowp-cTP_lowp)-((mincost_intervention_lowp/allnegatives)-cTN_lowp-cFN_lowp* /// 
allpositives/allnegatives)*(allnegatives/allpositives)/(cFN_lowp-cTP_lowp)



*hard cases less worth it
local p=`pvar'/5
local vD=100
local vP=0
local I=0.1

*decision making
*cost benchmark works as follows
*preventing conflict helps prevent massive damages (calculated for infinite horizon)
matrix costvector=(`vP',`vP'+`I',(1-`p')*`vD'+`p'*`vP'+`I',`vD')

local cTN=costvector[1,1]
local cFP = costvector[1,2]
local cTP = costvector[1,3]
local cFN = costvector[1,4]



*hard cases costs
generate costs_int_hard_lowp = negatives_hard*`cFP'+positives_hard*`cTP'+(allpositives_hard-positives_hard)*`cFN'+(allnegatives_hard-negatives_hard)*`cTN'
generate costs_noint_hard_lowp = (allpositives_hard)*`cFN'+(allnegatives_hard)*`cTN'

 

generate costsaving_hard_lowp=costs_int_hard_lowp-costs_noint_hard_lowp



*get cutoff value
egen mincost = min(costs_int)
egen mincost_lowp = min(costs_int_lowp)

*get cutoff value for hard
egen mincost_hard = min(costs_int_hard)
egen mincost_hard_lowp = min(costs_int_hard_lowp)

foreach j in costs_noint_hard_lowp costs_noint_hard costs_int_hard costs_int_hard_lowp mincost_hard mincost mincost_lowp costs_int costs_noint costs_int_lowp costs_noint_lowp {
	replace `j' =ln(`j'+1)
}



generate cutoff = mincost==costs_int
generate cutoff_lowp = mincost_lowp==costs_int_lowp

generate cutoffvalue = mine if cutoff==1
egen cutoffv = max(cutoffvalue)
summarize cutoffv
local cutoffv=r(mean)

generate cutoffvalue_lowp = mine if cutoff_lowp==1
egen cutoffv_lowp = max(cutoffvalue_lowp)
summarize cutoffv_lowp
local cutoffv_lowp = r(mean)







generate cutoff_hard = mincost_hard==costs_int_hard
generate cutoff_hard_lowp = mincost_hard_lowp==costs_int_hard_lowp

generate cutoffvalue_hard = mine if cutoff_hard==1
egen cutoffv_hard = max(cutoffvalue_hard)
summarize cutoffv_hard
local cutoffv_hard=r(mean)

generate cutoffvalue_hard_lowp = mine if cutoff_hard_lowp==1
egen cutoffv_hard_lowp = max(cutoffvalue_hard_lowp)
summarize cutoffv_hard_lowp
local cutoffv_hard_lowp = r(mean)

*numbers for paper
summarize mincost costs_noint mincost_lowp





gen ach = `cutoffv'


gen ei = 11.2 if _n == 1
replace ei = 11.6 if _n == 2

*Figure 9

graph drop _all
*cost figures any violence
twoway (line costs_int mine , lwidth(thick) lpattern(dash) connect(l) sort) (line costs_noint mine, lwidth(thick) lpattern(solid) connect(l) sort) ///
(line  ei ach, lpattern(dot) lwidth(thick) lc(black) connect(l) sort), ///
subtitle("Effective intervention", color(black)) ytitle("Log(total costs + 1)") xtitle(`"Cutoff"') legend( order(2 "No intervention benchmark" 1 "Intervention above cutoff" 3 "Cutoff") rows(1) size(small)) ///
plotregion(fcolor(white)) graphregion(fcolor(white)) name(figa)  

replace ach = `cutoffv_lowp'

twoway (line  costs_int_lowp mine , lwidth(thick) lpattern(dash) connect(l) sort) (line costs_noint_lowp mine, lwidth(thick) lpattern(solid) connect(l) sort) ///
(line  ei ach, lpattern(dot) lwidth(thick) lc(black) connect(l) sort), ///
subtitle("Ineffective intervention", color(black)) ytitle(`"Log(total costs + 1)"') xtitle(`"Cutoff"') legend( order(2 "No intervention benchmark" 1 "Interventon above cutoff" 3 "Cutoff") rows(1)  size(small)) ///
plotregion(fcolor(white)) graphregion(fcolor(white))  name(figb)


grc1leg figa figb, ycommon legendfrom(figa) plotregion(fcolor(white)) graphregion(fcolor(white))


graph export "Figures_superall/costs_allonsets_anyviolence.pdf" ,replace




replace costs_noint_hard_lowp=ln(costs_noint_hard_lowp)
replace costs_noint_hard=ln(costs_noint_hard)
replace costs_int_hard=ln(costs_int_hard)
replace costs_int_hard_lowp=ln(costs_int_hard_lowp)






generate cutoff_fpr = fpr if cutoff==1
egen cutoffv_fpr = max(cutoff_fpr)
summarize cutoffv_fpr
local cutoffv_fpr=r(mean)

generate cutoff_fpr_lowp = fpr if cutoff_lowp==1
egen cutoffv_fpr_lowp = max(cutoff_fpr_lowp)
summarize cutoffv_fpr_lowp
local cutoffv_fpr_lowp = r(mean)


*isocstcurves


* Figure 8
twoway (line tpr fpr, lwidth(thick) lpattern(solid) connect(l) sort) (line isocost_tpr fpr if isocost_tpr<=1, lwidth(thick) lpattern(dash)), ///
subtitle("Effective intervention", color(black)) ytitle(`"True Positive Rate"') xtitle(`"False Positive Rate"') legend( order(1 "ROC" 2 "Isocost curve") size(small)) ///
 aspectratio(1) name(fige) xline(`cutoffv_fpr', lwidth(thick) lpattern(dot) lcolor(black)) plotregion(fcolor(white)) graphregion(fcolor(white)) 



twoway (line tpr fpr, lwidth(thick) lpattern(solid) connect(l) sort) (line isocost_tpr_lowp fpr if isocost_tpr_lowp<=1, lwidth(thick) lcolor(maroon) lpattern(dash)), ///
subtitle("Ineffective intervention", color(black)) ytitle(`"True Positive Rate"') xtitle(`"False Positive Rate"') legend( order(1 "ROC" 2 "Isocost curve") size(small)) ///
 aspectratio(1) name(figf) xline(`cutoffv_fpr_lowp', lwidth(thick) lpattern(dot) lcolor(black)) plotregion(fcolor(white)) graphregion(fcolor(white)) 


grc1leg fige figf, legendfrom(fige) plotregion(fcolor(white)) graphregion(fcolor(white))

graph export "Figures_superall/costs_isocostcurves.pdf" ,replace



********************************************
*run below and decide on cutoff here
generate intervene = mine>=`cutoffv'
********************************************

replace intervene=. if mine==.
replace intervene=. if ons_anyviolence`f'==.

tab isocode if intervene==1 & textcases==1 & text!=.
tab isocode if intervene==1 & textcases==1 & text!=. & ons_anyviolence1==1


bysort time: egen intervene_ts=sum(intervene)
bysort time: egen onsets_ts=sum(ons_anyviolence`f')
bysort time: egen onsets_int_ts=sum(ons_anyviolence`f'*intervene)


preserve 
collapse (mean) intervene_ts onsets_ts onsets_int_ts, by(time)

*Figure D2
twoway (bar intervene_ts onsets_ts onsets_int_ts time, bfcolor(gray white black) blcolor(gray black black)  )  , ///
ytitle("Total cases") xtitle("Time") plotregion(fcolor(white))  graphregion(fcolor(white)) legend(order(1 "Interventions" 2 "Onsets" 3 "Onsets with intervention") cols(3))

graph export "Figures_superall/shareofanyviolence_bar.pdf",replace


twoway (bar intervene_ts onsets_ts onsets_int_ts time, fcolor(gray white black) blcolor(gray%0 black black)  )  , ///
subtitle("Effective interventions", color(black)) ytitle("Cases")  xlabel(#4) xtitle("Time") plotregion(fcolor(white))  ///
graphregion(fcolor(white)) legend(order(1 "Interventions" 2 "Onsets" 3 "Onsets with intervention") cols(3) size(small)) name(soft)

restore

tabulate isocode if intervene==1 & ons_anyviolence`f'==1

summarize intervene_ts


*make separation plot analysis
sort mine
generate id=_n


tabulate isocode if ons_anyviolence1==1 & id<10000

generate intervene_hard = mine>=`cutoffv_lowp'

replace intervene_hard=. if mine==.
replace intervene_hard=. if ons_anyviolence`f'==.
bysort time: egen intervene_hard_ts=sum(intervene_hard)
bysort time: egen hard_onsets_ts=sum(ons_anyviolence`f')
bysort time: egen honsets_int_ts=sum(intervene_hard*ons_anyviolence`f')


preserve 
collapse (mean) intervene_hard_ts hard_onsets_ts honsets_int_ts, by(time)

*Figure D3
twoway (bar intervene_hard_ts hard_onsets_ts honsets_int_ts time, bfcolor(gray white black) blcolor(gray black black)  )  , ///
ytitle("Total cases") xtitle("Time") plotregion(fcolor(white))  graphregion(fcolor(white)) legend(order(1 "Interventions" 2 "Onsets" 3 "Onsets with intervention") cols(3))

graph export "Figures_superall/shareofineffanyviolence_bar.pdf", replace






****Figure 1
clear
use masterfile_CR

xtset countryid ym


gen obs = 1

twoway (fpfitci ons_anyviolence1 anyviolence_dp if anyviolence_dp <= 24,clwidth(medthick) clcolor(black) ), ///
	xtitle("Months since last fatality") ytitle("Probability of any fatality") ///
	legend(off) plotregion(fcolor(white)) graphregion(fcolor(white))
graph export Figures_superall/relapse_JEEA.pdf, replace





****Figure 4


clear all
local topics=30


local f=1
local dependent="ons_anyviolence`f'"


insheet using "RandomForest1_3_200_0_superall_topics`topics'_0_`f'.csv", clear

keep if dep=="`dependent'"

matrix results=(0,0,0,0)
matrix cis=(0,0,0,0)

foreach j in text mine dummies {
	drop if `j' == .
}


forvalues timesince=0/240 {
	preserve
	keep if anyviolence_dp>=`timesince'
	rocreg `dependent' text mine dummies  if mine!=.,  nobootstrap
	matrix result = (e(b), `timesince')
	matrix results=results\result


	restore
}



clear
svmat results
drop in 1
rename results1 text
rename results2 both
rename results3 history
rename results4 monthssince




twoway (line text history both monthssince, lcolor(green orange navy) lpattern(shortdash dash solid) lwidth(medthick medthick medthick)) ///
	if monthssince<241, ///
ytitle(`"AUC (ROC)"') xtitle(`"Months since last conflict"') legend( order(1 "Text" 2 "History" 3 "Text & history") size(small) rows(1) ) xlabel(0(40)240) ylabel(0.5(0.1)1)  ///
plotregion(fcolor(white)) graphregion(fcolor(white))


graph export "Figures_superall/auc_continuous.pdf" ,replace





****Figure E6


clear all
local topics=30


local f=1



foreach dependent in ons_ged_best_ns1 ons_ged_best_os1 ons_ged_best_sb1 {


	clear
	insheet using "RandomForest1_3_200_0_superall_topics`topics'_2_`f'.csv", clear
	
	foreach j in text mine dummies {
		drop if `j' == .
	}

	keep if dep=="`dependent'"

	matrix results=(0,0,0,0)
	
	
	forvalues timesince=0/240 {
		preserve
		keep if anyviolence_dp>=`timesince'
		rocreg `dependent' text mine dummies ,  nobootstrap
		matrix result = (e(b), `timesince')
		matrix results=results\result



		restore
	}
	
	
	clear
	svmat results
	drop in 1
	rename results1 text
	rename results2 both
	rename results3 history
	rename results4 monthssince
	gen dep = "`dependent'"
	
	save `dependent'_cont, replace
}

clear
foreach dependent in ons_ged_best_ns1 ons_ged_best_os1 ons_ged_best_sb1 {
	append using `dependent'_cont
	erase `dependent'_cont.dta
}
	

	



graph drop _all
foreach dependent in ons_ged_best_ns1 ons_ged_best_os1 ons_ged_best_sb1 {
	
	if "`dependent'" == "ons_ged_best_ns1" {
		local s Non-state
	}
	if "`dependent'" == "ons_ged_best_os1" {
		local s One-sided
	}
	if "`dependent'" == "ons_ged_best_sb1" {
		local s State-based
	}

	twoway (line text history both monthssince, lcolor(green orange navy) lpattern(shortdash dash solid) lwidth(medthick medthick medthick)) ///
		if monthssince<241 & dep == "`dependent'", ///
	ytitle(`"AUC (ROC)"') xtitle(`"Months since last conflict"') legend( order(1 "Text" 2 "History" 3 "Text & history") size(small) rows(1) ) xlabel(0(40)240) ylabel(0.2(0.2)1) subtitle("`s'")  ///
	plotregion(fcolor(white)) graphregion(fcolor(white))  /// 
	name(`dependent')
}

 
 graph combine ons_ged_best_ns1 ons_ged_best_os1 ons_ged_best_sb1, rows(1)   plotregion(fcolor(white)) graphregion(fcolor(white)) xsize(10)

graph export "Figures_superall/auc_continuous_other_conflicts.pdf" ,replace







*Figure 5
foreach j in anyviolence  {
	clear all
	
	insheet using merged_1_3_topics30_200/merged_1_3_topics30_200_m8_y2020.csv
	
	gen uff = 0 if ons_`j'1 == 0
	replace uff = 1 if ons_`j'1  == 1 & anyviolence_dp <= 120
	replace uff = 2 if ons_`j'1  == 1 & anyviolence_dp > 120 & anyviolence_dp != .
	collapse ste_theta* , by(uff)

	reshape long ste_theta, i(uff) j(topic)

	replace topic = topic + 1
	
	
	
	gen topicx = "Justice" if topic == 1
	replace topicx = "Middle East" if topic == 2
	replace topicx = "Economics" if topic == 3
	replace topicx = "Current events" if topic == 4
	replace topicx = "Industry" if topic == 5
	replace topicx = "Nuclear treaties" if topic == 6
	replace topicx = "Military" if topic == 7
	replace topicx = "Urban" if topic == 8
	replace topicx = "Intelligence" if topic == 9
	replace topicx = "Russia" if topic == 10
	replace topicx = "Transport" if topic == 11
	replace topicx = "Peace agreements" if topic == 12
	replace topicx = "Daily life" if topic == 13
	replace topicx = "State visits" if topic == 14
	replace topicx = "Health" if topic == 15	
	replace topicx = "International relations" if topic == 16
	replace topicx = "Politics" if topic == 17
	replace topicx = "Religion" if topic == 18
	replace topicx = "Middle East II" if topic == 19
	replace topicx = "Activism" if topic == 20
	replace topicx = "Finance" if topic == 21
	replace topicx = "Parliament" if topic == 22
	replace topicx = "China" if topic == 23
	replace topicx = "South America" if topic == 24
	replace topicx = "Terror" if topic == 25
	replace topicx = "Media" if topic == 26
	replace topicx = "Trade" if topic == 27
	replace topicx = "Political power" if topic == 28
	replace topicx = "India-Pakistan" if topic == 29	
	replace topicx = "Borders" if topic == 30	
	


	
	gen topic0 = ste_theta if uff == 0
	gen topic1 = ste_theta if uff == 1
	gen topic2 = ste_theta if uff == 2
	gen ei = 0
	collapse topic0 topic1 topic2 ,by(topicx)
	replace topic1 = topic1/topic0
	replace topic2 = topic2/topic0

	twoway (scatter  topic2 topic1, mlabel(topicx) )
	gen ach = (_n-1)/30 * 0.875 + 0.725
	replace ach = 0.5 if _n == 1
	replace ach = 2 if _n == 30

	gen pos = 3
	replace pos = 9 if topicx == "Daily life"
	replace pos = 5 if topicx == "Administration"
	replace pos = 5 if topicx == "Internet"
	replace pos = 9 if topicx == "Terror"
	replace pos = 9 if topicx == "Peace agreements"
	replace pos = 6 if topicx == "International relations"
	replace pos = 9 if topicx == "Transport"
	replace pos = 9 if topicx == "Politics"
	replace pos = 12 if topicx == "Current events"
	replace pos = 10 if topicx == "Finance"
	replace pos = 5 if topicx == "Economics"
	replace pos = 12 if topicx == "State visits"
	replace pos = 4 if topicx == "Industry"
	replace pos = 1 if topicx == "Activism"
	replace pos = 2 if topicx == "Military"
	replace pos = 4 if topicx == "Parlament"
	replace pos = 10 if topicx == "Media"
	replace pos = 12 if topicx == "Health"
	replace pos = 9 if topicx == "Borders"
	replace pos = 5 if topicx == "Nuclear treaties"
	replace pos = 2 if topicx == "Political power"
	
	
	

	twoway (scatter  topic2 topic1, mlabel(topicx) mlabv(pos)) (line ach ach, lcolor(gray) lpattern(dash)),	///
			xline(1) yline(1) legend(off) ytitle("Relative appearance before non-hard onset") xtitle("Relative appearance before hard onset") plotregion(fcolor(white)) graphregion(fcolor(white)) 
			graph export figures_superall/`j'_topics_rel.pdf, replace
			
	
	
}



*****


clear all


local f = 1
local topics=30
insheet using "RandomForest1_3_200_0_superall_topics30_0_`f'.csv", clear

keep if dep=="ons_anyviolence`f'"

replace mine=mine*100
replace dummies=dummies*100
replace text=text*100


generate intervene=mine>100*0.0472676 
replace intervene=. if mine==.

generate interventionline=100*0.0472676 

tab intervene ons_anyviolence1 if anyviolence_dp>23
tab intervene ons_anyviolence1 if anyviolence_dp>119

tab isocode if intervene==1 &  anyviolence_dp>23

generate ym=ym(year,  month)
format ym %tm

*Figure 10

graph drop _all

twoway (line mine ym, lcolor(maroon) lwidth(thick)) (line interventionline ym, lwidth(thick) lpattern(dash) lcolor(black)) if isocode=="LSO" & year>2009, ///
subtitle("Lesotho", color(black)) ytitle(`"Risk (in %)"')  legend( order(1 "Forecast" 2 "Intervention threshold") size(small)) xtitle("") ///
plotregion(fcolor(white)) graphregion(fcolor(white)) name(figa_a) xlabel(,  labsize(small))  


twoway (line mine ym, lcolor(maroon) lwidth(thick)) (line interventionline ym, lwidth(thick) lpattern(dash) lcolor(black)) if isocode=="BLR" & year>2009, ///
subtitle("Belarus", color(black)) ytitle(`"Risk (in %)"')  legend( order(1 "Forecast" 2 "Intervention threshold") size(small)) xtitle("") ///
plotregion(fcolor(white)) graphregion(fcolor(white)) name(figa_b) xlabel(,  labsize(small))  
 
grc1leg figa_a figa_b, legendfrom(figa_a) plotregion(fcolor(white)) graphregion(fcolor(white))

graph export "Figures_superall/case_lso_blr.pdf" ,replace

*Figure 11

graph drop _all
twoway (line mine ym, lcolor(maroon) lwidth(thick)) (line interventionline ym, lwidth(thick) lpattern(dash) lcolor(black)) if isocode=="TUN" & year>2009, ///
subtitle("Tunisia", color(black)) ytitle(`"Risk (in %)"')  legend( order(1 "Forecast" 2 "Intervention threshold") size(small)) xtitle("") ///
plotregion(fcolor(white)) graphregion(fcolor(white)) name(figa_a) xlabel(,  labsize(small))  

twoway (line mine ym, lcolor(maroon) lwidth(thick)) (line interventionline ym, lwidth(thick) lpattern(dash) lcolor(black)) if isocode=="CMR" & year>2009, ///
subtitle("Cameroon", color(black)) ytitle(`"Risk (in %)"')  legend( order(1 "Forecast" 2 "Intervention threshold") size(small)) xtitle("") ///
plotregion(fcolor(white)) graphregion(fcolor(white)) name(figa_b) xlabel(,  labsize(small))  

grc1leg figa_a figa_b, legendfrom(figa_a) plotregion(fcolor(white)) graphregion(fcolor(white))

graph export "Figures_superall/case_tun_cmr.pdf", replace



****Appendix Figure A1
clear all

use "data_info_all_svi.dta"



gen obs = 1
gen timeq = yq(year,quarter)
format %tq timeq
collapse (max) quarter (sum) obs  , by(year timeq  paper)


keep if year >= 1980

drop if year == 2020 & quarter >= 3

gen hans = 0



local w timeq
twoway (line obs `w' if paper == "BBC", yaxis(2) yscale(range(0) axis(2)) lcolor(gs0) ) ///
	(line obs `w' if paper == "NYT", yscale(range(0) axis(1)) lcolor(gs10) lpattern(dash)) ///
	(line obs `w' if paper == "WashingtonPost", lcolor(gs0) lpattern(dash)) ///
	(line obs `w' if paper == "Economist", lcolor(gs10) lpattern(solid))  ///	
	(line obs `w' if paper == "LN", lcolor(black) lpattern(dot)),  ///	
	legend(order (1 "BBC" 2 "NYT" 3 "WP" 4 "Economist" 5 "LatinNews")  size(small) position(6) rows(1))  ///
	 plotregion(fcolor(white)) graphregion(fcolor(white)) ytitle("") ytitle("BBC", axis(2)) ytitle("Other papers", axis(1))  xtitle("") ///
	xla(#5)
	

graph export Figures_superall/number_of_articles.pdf , replace 


****Figure of B2

clear all


local tops 30
insheet using merged_1_3_topics30_200/merged_1_3_topics30_200_m8_y2020.csv


gen timeq = ym(year, month)

format timeq %tm
keep if isocode == "GBR" | isocode == "USA"

local military ste_theta16

graph drop _all
twoway (line `military' timeq if year >= 2005,  lcolor( blue) lpattern(solid )) if isocode=="GBR", ///
ytitle("") xtitle("") subtitle("UK", color(black) ) yscale(range(0 0.2) ) yla(0(0.05)0.2)  xlabel(#6) plotregion(fcolor(white)) graphregion(fcolor(white)) legend(off) name(fig1)

twoway (line `military' timeq if year >= 2005,  lcolor( blue) lpattern(solid )) if isocode=="USA", ///
ytitle("") xtitle("") subtitle("USA", color(black) ) yscale(range(0 0.1) ) yla(0(0.02)0.1)  xlabel(#6) plotregion(fcolor(white)) graphregion(fcolor(white)) legend(off) name(fig2)

graph combine fig1 fig2, rows(2) plotregion(fcolor(white)) graphregion(fcolor(white))
graph export Figures_superall/politics_US_UK.pdf, replace


****Figure B1


clear all


local tops 30
insheet using merged_1_3_topics30_200/merged_1_3_topics30_200_m8_y2020.csv

graph drop _all

gen timeq = ym(year, month)

format timeq %tm

local military ste_theta24 ste_theta2 

twoway (line `military' timeq,  lcolor(maroon blue) lpattern(solid dash)) if isocode=="BRA", ///
ytitle("") xtitle("") subtitle("Brazil", color(black) ) yscale(range(0 0.3) ) yla(0(0.1)0.3)  xlabel(#6) plotregion(fcolor(white)) graphregion(fcolor(white)) legend(off) name(fig1)


twoway (line `military' timeq,  lcolor(maroon blue) lpattern(solid dash))  if isocode=="IRQ", ///
ytitle("") xtitle("") subtitle("Iraq", color(black) ) yscale(range(0 0.3) ) yla(0(0.1)0.3)  xlabel(#6) plotregion(fcolor(white)) graphregion(fcolor(white)) legend(off) name(fig2)


twoway (line `military' timeq,  lcolor(maroon blue) lpattern(solid dash)) if isocode=="UKR", ///
ytitle("") xtitle("") subtitle("Ukraine", color(black) ) yscale(range(0 0.3) ) yla(0(0.1)0.3)  xlabel(#6) plotregion(fcolor(white)) graphregion(fcolor(white)) legend(off) name(fig3)


twoway (line `military' timeq,  lcolor(maroon blue) lpattern(solid dash))  if isocode=="AGO", ///
ytitle("") xtitle("") subtitle("Angola", color(black) ) yscale(range(0 0.3) ) yla(0(0.1)0.3) xlabel(#6) plotregion(fcolor(white)) graphregion(fcolor(white)) legend(order(1 "Terror" 2 "Economics")) name(fig4)


quietly grc1leg fig1 fig4 fig2 fig3,  legendfrom(fig4)  plotregion(fcolor(white)) graphregion(fcolor(white)) 
graph export Figures_superall/cases_terror_economy.pdf , replace 
